//
//  SXPretweetView.swift
//  weibo
//
//  Created by shixinPeng on 16/3/2.
//  Copyright © 2016年 shixinPeng. All rights reserved.
//

import UIKit
import SnapKit

class SXPretweetView: UIView {

    //Constraint
    var bottomConstraint: Constraint?
    ///  转发微博的属性
    var retweetViewModel:SXPstatusesViewModel?{
        didSet{
            
            if let text = retweetViewModel?.retweetStatusString{
                
                contentLabel.attributedText = SXPstatusesViewModel().getAttributdString(text)
            }
            
            
            pictureView.pictureCollectionViewModel = retweetViewModel
            
            //移除原有约束
            self.bottomConstraint?.uninstall()
            if let pic_Url = retweetViewModel?.statusesModel?.retweeted_status?.pic_urls where pic_Url.count > 0{//有值
                
                pictureView.hidden = false
                //修改约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                //传值
                //把包装成viewmodel 的数组传给pictureList
                //临时数组
                var result = [SXPpictureViewModel]()
                
                //遍历数组
                for model in pic_Url {
                    
                    let viewModel = SXPpictureViewModel()
                    
                    viewModel.pictureModel = model
                    //添加到临时数组
                    result.append(viewModel)
                }
                //传值
                pictureView.pictureList = result
                
            }else{
                //没有配图
                pictureView.hidden = true
                //修改约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
                
            }
            
            
            
            
        }
    }
    
    //1.重写初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
      
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
  
    }
    //2.私有方法,加载控件,添加约束
    private func setupUI() {
        //添加控件
        addSubview(contentLabel)
        
        addSubview(pictureView)
        
        //添加约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            //make.bottom.equalTo(self.snp_bottom)
            make.right.equalTo(self.snp_right)
        }
        //添加转发配图约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom)
            make.left.equalTo(self.snp_left)
        }
        //添加自身约束
        self.snp_makeConstraints { (make) -> Void in
            
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
    }
    //3.懒加载
    /// 微博转载内容
    private lazy var contentLabel:UILabel = {
        let lab = UILabel()
        
        lab.text = "微博转载的内容"
        
        lab.font = UIFont.systemFontOfSize(14)
        
        lab.backgroundColor = UIColor.orangeColor()
        
        lab.numberOfLines = 0
        
        return lab
        
    }()
    //配图视图
    private lazy var pictureView:SXPpictureView = SXPpictureView()
}
